package com.xcity.game.task;

import org.slf4j.Logger;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.xcity.db.entity.Player;
import com.xcity.game.common.Cause;
import com.xcity.game.common.ItemResponse;
import com.xcity.pomelo.push.SyncPush;
import com.xcity.pomelo.push.msg.DotTipPush;
import com.xcity.pomelo.push.sync.MapSync;

import naga.x.App;
import naga.x.game.template.TemplateService;
import naga.x.game.time.TimeUpdater;

public class Task {

	@JSONField(name = "1")
	private int id;
	@JSONField(name = "2")
	private State state;
	@JSONField(name = "3")
	private int progress;
	@JSONField(name = "4")
	private long createTime;
	@JSONField(name = "5")
	private JSONObject archive; // 存档
	
	@JSONField(serialize = false)
	private TaskTemplate template;
	@JSONField(serialize = false)
	private TaskManager vm;
	
	public static final byte TYPE_MAIN = 0, TYPE_DAILY = 1;
	protected static final Logger LOG = TaskService.LOG;
	
	public static enum State {
		INIT, // 正在进行
		FINISHED, // 已完成未领奖
		REWARDED // 已完成并已领奖
	}
	
	public Task() {}
	
	public Task(TaskManager vm, TaskTemplate template) {
		this.vm = vm;
		this.id = template.getId();
		this.state = State.INIT;
		this.progress = 0;
		this.createTime = TimeUpdater.getInstance().now();
		this.template = template;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public State getState() {
		return state;
	}

	public void setState(State state) {
		this.state = state;
	}

	public int getProgress() {
		return progress;
	}

	public void setProgress(int progress) {
		this.progress = progress;
	}

	public long getCreateTime() {
		return createTime;
	}

	public void setCreateTime(long createTime) {
		this.createTime = createTime;
	}
	
	public JSONObject getArchive() {
		return archive != null ? archive : (archive = new JSONObject());
	}

	public void setArchive(JSONObject archive) {
		this.archive = archive;
	}

	public TaskTemplate getTemplate() {
		return template != null ? template : (template = App.getApp().getServiceManager().get(TemplateService.class).get(TaskTemplate.class, id));
	}

	public void setTemplate(TaskTemplate template) {
		this.template = template;
	}

	public TaskManager getVm() {
		return vm;
	}

	public void setVm(TaskManager vm) {
		this.vm = vm;
	}
	
	public void reinit(TaskTemplate template) {
		moveToNext(template);
		this.archive = new JSONObject();
		Player player = getVm().getOwner();
		player.getTasks().trigger(player);
		LOG.info("[TASK REINIT]ID[{}]TASK[{}]", player.getId(), id);
	}
	
	public void moveToNext(TaskTemplate next) {
		this.id = next.getId();
		this.state = State.INIT;
		this.template = next;
		this.createTime = TimeUpdater.getInstance().now();
	}
	
	/**
	 * version3的日常任务用这个重置
	 */
	public void resetDayTask(){
		this.state = State.INIT;
		this.createTime = TimeUpdater.getInstance().now();
		this.archive = new JSONObject();
	}
	
	public void finish(String cause) {
		if (state == State.INIT) {
			setState(State.FINISHED);
			Player player = getVm().getOwner();
			player.send(DotTipPush.TASKS_FINISHED);
			player.send(new MapSync(SyncPush.SYNC_TASK, id, state));
			LOG.info("[TASK FINISH]ID[{}]TASK[{}]CAUSE[{}]", player.getId(), id, cause);
		}
	}
	
	public void reward(String cause) {
		if (state == State.FINISHED) {
			state = State.REWARDED;
			Player player = getVm().getOwner();
			TaskTemplate template = getTemplate();
			player.addExp(template.getExp(), Cause.TASK_REWARD);
			player.addGold(template.getGold(), Cause.TASK_REWARD);
			player.addStone(template.getStone(), Cause.TASK_REWARD);
			player.addHonor(template.getHonor(), Cause.TASK_REWARD);
			ItemResponse[] items = template.getItemObjs();
			for (ItemResponse item : items) {
				player.getBag().addItemWithoutException(item.getId(), item.getCount(), Cause.TASK_REWARD);
			}
			LOG.info("[TASK REWARD]ID[{}]TASK[{}]CAUSE[{}]", player.getId(), id, cause);
		}
	}

//	public void addProgress(Player owner, int val) {
//		if (state == State.INIT && val > 0) {
//			val += progress;
//			replaceProgress(owner, val);
//		}
//	}
//	
//	public boolean forceComplete(Player owner) {
//		return replaceProgress(owner, template.getCondition());
//	}
//	
//	public boolean replaceProgress(Player owner, int val) {
//		if (state != State.INIT) {
//			return false;
//		}
//		this.progress = val;
//		if (this.progress >= template.getCondition()) {
//			this.progress = template.getCondition();
//			this.state = State.FINISHED;
//			App.getApp().getEventManager().addEvent(GameEvents.EVENT_USE_SYSTEM, owner, ISystem.SYS_TASK);
//		}
//		return true;
//	}
	
}
